package com.xiaoge.biz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoge.auth.annotation.ControllerEndpoint;
import com.xiaoge.biz.entity.Product;
import com.xiaoge.biz.service.ProductService;
import com.xiaoge.biz.vo.ProductVo;
import com.xiaoge.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xiaoge
 * @since 2021-03-21
 */
@RestController
@CrossOrigin
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;


    /**
     * 分页查询物资
     *
     * @param current
     * @param size
     * @param ProductVo
     * @return
     */
    @ApiOperation(value = "分页查询物资")
    @PostMapping("/getProductList")
    public Result getProductList(@RequestParam(defaultValue = "1") Integer current,
                                 @RequestParam(defaultValue = "6") Integer size,
                                 @RequestBody ProductVo ProductVo) {
        //取得分页条件
        Page<Product> page = new Page<>(current, size);
        //获得wragger
        QueryWrapper<Product> queryWrapper = getWrapper(ProductVo);

        //执行查询
        IPage<Product> productPage = productService.page(page, queryWrapper);

        long total = productPage.getTotal();
        List<Product> records = productPage.getRecords();
        return Result.ok().data("total", total).data("records", records);
    }

    /**
     * 分页查询库存
     *
     * @param current
     * @param size
     * @param ProductVo
     * @return
     */
    @ApiOperation(value = "分页查询库存")
    @PostMapping("/findProductStocks")
    public Result findProductStocks(@RequestParam(defaultValue = "1") Integer current,
                                 @RequestParam(defaultValue = "6") Integer size,
                                 @RequestBody ProductVo ProductVo) {
        //取得分页条件
        Page<Product> page = new Page<>(current, size);
        //获得wragger
        QueryWrapper<Product> queryWrapper = getWrapper(ProductVo);
        //执行查询
        IPage<Product> productPage = productService.findProductStocks(page, queryWrapper);

        long total = productPage.getTotal();
        List<Product> records = productPage.getRecords();
        return Result.ok().data("total", total).data("records", records);
    }

    /**
     * 分页查询可出库的库存
     *
     * @param current
     * @param size
     * @param ProductVo
     * @return
     */
    @ApiOperation(value = "分页查询可出库的库存")
    @PostMapping("/findOutEnableStocks")
    public Result findOutEnableStocks(@RequestParam(defaultValue = "1") Integer current,
                                    @RequestParam(defaultValue = "6") Integer size,
                                    @RequestBody ProductVo ProductVo) {
        //取得分页条件
        Page<Product> page = new Page<>(current, size);
        //获得wragger
        QueryWrapper<Product> queryWrapper = getWrapper(ProductVo);
        //执行查询
        IPage<Product> productPage = productService.findOutEnableStocks(page, queryWrapper);

        long total = productPage.getTotal();
        List<Product> records = productPage.getRecords();
        return Result.ok().data("total", total).data("records", records);
    }

    /**
     * 添加物资
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加物资失败", operation = "添加物资")
    @ApiOperation(value = "添加物资")
    @PostMapping("/addProduct")
    public Result addProduct(@RequestBody Product product) {
        productService.add(product);
        return Result.ok();
    }

    /**
     * 修改物资信息
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "修改物资失败", operation = "修改物资")
    @ApiOperation(value = "修改物资")
    @PostMapping("/updateProduct")
    public Result updateProduct(@RequestBody Product product) {
        productService.updateById(product);
        return Result.ok();
    }

    /**
     * 恢复物资
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "恢复物资失败", operation = "恢复物资")
    @ApiOperation(value = "恢复物资")
    @PostMapping("/backProduct")
    public Result backProduct(Long id) {
        Product product = new Product();
        product.setId(id);
        product.setStatus(0);
        productService.updateById(product);
        return Result.ok();
    }

    /**
     * 移至回收站
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "移至回收站失败", operation = "移至回收站")
    @ApiOperation(value = "恢复物资")
    @PostMapping("/removeProduct")
    public Result removeProduct(Long id) {
        Product product = new Product();
        product.setId(id);
        product.setStatus(1);
        productService.updateById(product);
        return Result.ok();
    }

    /**
     * 审核通过
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "审核失败", operation = "审核通过")
    @ApiOperation(value = "审核通过")
    @PostMapping("/publishProduct")
    public Result publishProduct(Long id) {
        Product product = new Product();
        product.setId(id);
        product.setStatus(0);
        productService.updateById(product);
        return Result.ok();
    }

    /**
     * 删除物资
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除物资失败", operation = "删除物资")
    @ApiOperation(value = "删除物资")
    @DeleteMapping("/delProduct")
    public Result delProduct(Long id) {
        productService.removeById(id);
        return Result.ok();
    }


    private QueryWrapper<Product> getWrapper(ProductVo productVo) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        if (productVo != null) {

            //查询每个条件
            if (!ObjectUtils.isEmpty(productVo.getName())) {
                queryWrapper.like("name", productVo.getName());
            }
            if (!ObjectUtils.isEmpty(productVo.getCategoryKeys()) && !productVo.getCategoryKeys().equals("")) {
                List<String> keys = Arrays.asList(productVo.getCategoryKeys().split(","));
                queryWrapper.in("three_category_id", keys);
            }
            if (!ObjectUtils.isEmpty(productVo.getStatus())) {
                queryWrapper.eq("status", productVo.getStatus());
            }
        }
        return queryWrapper;
    }
}
